package com.sgb.goods.view.activity.inquiry;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityFindInquirySupplierBinding;
import com.sgb.goods.entity.inquiry.SupplierEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.viewmodel.inquiry.FindInquirySupplierModel;
import com.swgk.core.util.LogUtil;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 * 描述： 发布询价-供应商选择
 *
 * @author qiangHeNoo
 * <p>
 * 时间：2021.12.14
 */
@Route(path = Constants.ARouter.AR_FIND_INQUIRY_SUPPLIER)
public class FindInquirySupplierActivity extends BaseActivity<GoodsActivityFindInquirySupplierBinding, FindInquirySupplierModel> implements NewFindInquirySupplierAdapter.OnSupplierItemClickListener {


    public NewFindInquirySupplierAdapter marketSupplierAdapter;
    public NewFindInquirySupplierAdapter vipMemberSupplierAdapter;
    public NewFindInquirySupplierAdapter memberSupplierAdapter;
    private ArrayList<SupplierEntity> marketSelectedSupplierList = new ArrayList<>();
    private ArrayList<SupplierEntity> vipMemberSelectedSupplierList = new ArrayList<>();
    private ArrayList<SupplierEntity> memberSelectedSupplierList = new ArrayList<>();
    private ArrayList<SupplierEntity> allSelectedSupplierList = new ArrayList<>();
    public static final String SUPPLIER_ENTITY = "CALLBACK_ENTITY";
    private String searchSupplierName = "";
    private String clickFlag = "0";
    private boolean marketLoadMore = false;
    private boolean vipMemberLoadMore = false;
    private boolean memberLoadMore = false;
    int loginIdentityType;

    List<SupplierEntity> intentsSupplierEntityList;
    List<String> filterCompanyNos = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_find_inquiry_supplier;
    }

    @Override
    protected void initObserve() {
        mViewModel.marketSupplierEntity.observe(FindInquirySupplierActivity.this, this::marketSupplierList);
        mViewModel.vipMemberSupplierEntity.observe(FindInquirySupplierActivity.this, this::vipMemberSupplierList);
        mViewModel.memberSupplierEntity.observe(FindInquirySupplierActivity.this, this::memberSupplierList);
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(FindInquirySupplierModel.class, BR.viewModel);
    }

    @Override
    protected void init() {
        //类型 0个人 1企业 2团队 3组织
        loginIdentityType = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        intentsSupplierEntityList = (List<SupplierEntity>) getIntent().getSerializableExtra("newData");
        if (intentsSupplierEntityList != null && intentsSupplierEntityList.size() > 0) {
            for (int i = 0; i < intentsSupplierEntityList.size(); i++) {
                filterCompanyNos.add(intentsSupplierEntityList.get(i).getNo());
            }
        }
        if (loginIdentityType == 1) {
            mDataBinding.llTitleView.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.llTitleView.setVisibility(View.GONE);
        }

        if (marketSupplierAdapter == null) {
            marketSupplierAdapter = new NewFindInquirySupplierAdapter();
            marketSupplierAdapter.setOnItemClickListener(this);
        }
        mDataBinding.marketRecyclerView.setAdapter(marketSupplierAdapter);
        mDataBinding.marketRefreshLayout.setEnableLoadMore(true);
        mDataBinding.marketRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                marketLoadMore = true;
                mViewModel.getMarketSupplier(searchSupplierName, filterCompanyNos, true);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                marketLoadMore = false;
                mViewModel.getMarketSupplier(searchSupplierName, filterCompanyNos, false);
            }
        });
        mViewModel.getMarketSupplier(searchSupplierName, filterCompanyNos, marketLoadMore);

        if (vipMemberSupplierAdapter == null) {
            vipMemberSupplierAdapter = new NewFindInquirySupplierAdapter();
            vipMemberSupplierAdapter.setOnItemClickListener(this);
        }
        mDataBinding.vipMemberRecyclerView.setAdapter(vipMemberSupplierAdapter);
        mDataBinding.vipMemberRefreshLayout.setEnableLoadMore(true);
        mDataBinding.vipMemberRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                vipMemberLoadMore = true;
                mViewModel.getVipMemberSupplier(searchSupplierName, filterCompanyNos, true);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                vipMemberLoadMore = false;
                mViewModel.getVipMemberSupplier(searchSupplierName, filterCompanyNos, false);
            }
        });
        mViewModel.getVipMemberSupplier(searchSupplierName, filterCompanyNos, vipMemberLoadMore);

        if (memberSupplierAdapter == null) {
            memberSupplierAdapter = new NewFindInquirySupplierAdapter();
            memberSupplierAdapter.setOnItemClickListener(this);
        }
        mDataBinding.memberRecyclerView.setAdapter(memberSupplierAdapter);
        mDataBinding.memberRefreshLayout.setEnableLoadMore(true);
        mDataBinding.memberRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                memberLoadMore = true;
                mViewModel.getMemberSupplier(searchSupplierName, filterCompanyNos, true);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                memberLoadMore = false;
                mViewModel.getMemberSupplier(searchSupplierName, filterCompanyNos, false);
            }
        });
        mViewModel.getMemberSupplier(searchSupplierName, filterCompanyNos, memberLoadMore);
    }

    @Override
    protected void initListener() {
        mDataBinding.ivBack.setOnClickListener(v -> finish());
        mDataBinding.right.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.putExtra(SUPPLIER_ENTITY, allSelectedSupplierList);
            setResult(RESULT_OK, intent);
            finish();
        });
        mDataBinding.search.setOnClickListener(v -> {
            KeyboardUtils.hideSoftInput(mDataBinding.goodsSearchSupplier);
            searchSupplierName = mDataBinding.goodsSearchSupplier.getText().toString().trim();
            if ("0".equals(clickFlag)) {
                mViewModel.getMarketSupplier(searchSupplierName, filterCompanyNos, marketLoadMore);
            } else if ("1".equals(clickFlag)) {
                mViewModel.getVipMemberSupplier(searchSupplierName, filterCompanyNos, vipMemberLoadMore);
            } else if ("2".equals(clickFlag)) {
                mViewModel.getMemberSupplier(searchSupplierName, filterCompanyNos, memberLoadMore);
            }
        });
        mDataBinding.goodsSearchSupplier.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        mDataBinding.goodsSearchSupplier.setOnKeyListener((v, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                KeyboardUtils.hideSoftInput(mDataBinding.goodsSearchSupplier);
                searchSupplierName = mDataBinding.goodsSearchSupplier.getText().toString().trim();
                if ("0".equals(clickFlag)) {
                    mViewModel.getMarketSupplier(searchSupplierName, filterCompanyNos, marketLoadMore);
                } else if ("1".equals(clickFlag)) {
                    mViewModel.getVipMemberSupplier(searchSupplierName, filterCompanyNos, vipMemberLoadMore);
                } else if ("2".equals(clickFlag)) {
                    mViewModel.getMemberSupplier(searchSupplierName, filterCompanyNos, memberLoadMore);
                }
                return true;
            }

            return false;
        });
        //市场客户
        mDataBinding.marketLayout.setOnClickListener(v -> {
            KeyboardUtils.hideSoftInput(mDataBinding.goodsSearchSupplier);
            clickFlag = "0";
            mDataBinding.marketView.setTextColor(ColorUtils.getColor(R.color.goods_color_FFEF4033));
            mDataBinding.marketLine.setBackgroundColor(ColorUtils.getColor(R.color.goods_color_FFEF4033));
            mDataBinding.vipMemberView.setTextColor(ColorUtils.getColor(R.color.goods_color_FF969696));
            mDataBinding.vipMemberLine.setBackgroundColor(ColorUtils.getColor(R.color.goods_color_FFFFFFFF));
            mDataBinding.memberView.setTextColor(ColorUtils.getColor(R.color.goods_color_FF969696));
            mDataBinding.memberLine.setBackgroundColor(ColorUtils.getColor(R.color.goods_color_FFFFFFFF));
            mDataBinding.marketBottomLayout.setVisibility(View.VISIBLE);
            mDataBinding.vipMemberBottomLayout.setVisibility(View.GONE);
            mDataBinding.memberBottomLayout.setVisibility(View.GONE);
        });
        //会员客户
        mDataBinding.vipMemberLayout.setOnClickListener(v -> {
            KeyboardUtils.hideSoftInput(mDataBinding.goodsSearchSupplier);
            clickFlag = "1";
            mDataBinding.marketView.setTextColor(ColorUtils.getColor(R.color.goods_color_FF969696));
            mDataBinding.marketLine.setBackgroundColor(ColorUtils.getColor(R.color.goods_color_FFFFFFFF));
            mDataBinding.vipMemberView.setTextColor(ColorUtils.getColor(R.color.goods_color_FFEF4033));
            mDataBinding.vipMemberLine.setBackgroundColor(ColorUtils.getColor(R.color.goods_color_FFEF4033));
            mDataBinding.memberView.setTextColor(ColorUtils.getColor(R.color.goods_color_FF969696));
            mDataBinding.memberLine.setBackgroundColor(ColorUtils.getColor(R.color.goods_color_FFFFFFFF));
            mDataBinding.marketBottomLayout.setVisibility(View.GONE);
            mDataBinding.vipMemberBottomLayout.setVisibility(View.VISIBLE);
            mDataBinding.memberBottomLayout.setVisibility(View.GONE);
        });
        //成员客户
        mDataBinding.memberLayout.setOnClickListener(v -> {
            KeyboardUtils.hideSoftInput(mDataBinding.goodsSearchSupplier);
            clickFlag = "2";
            mDataBinding.marketView.setTextColor(ColorUtils.getColor(R.color.goods_color_FF969696));
            mDataBinding.marketLine.setBackgroundColor(ColorUtils.getColor(R.color.goods_color_FFFFFFFF));
            mDataBinding.vipMemberView.setTextColor(ColorUtils.getColor(R.color.goods_color_FF969696));
            mDataBinding.vipMemberLine.setBackgroundColor(ColorUtils.getColor(R.color.goods_color_FFFFFFFF));
            mDataBinding.memberView.setTextColor(ColorUtils.getColor(R.color.goods_color_FFEF4033));
            mDataBinding.memberLine.setBackgroundColor(ColorUtils.getColor(R.color.goods_color_FFEF4033));
            mDataBinding.marketBottomLayout.setVisibility(View.GONE);
            mDataBinding.vipMemberBottomLayout.setVisibility(View.GONE);
            mDataBinding.memberBottomLayout.setVisibility(View.VISIBLE);
        });
    }

    private void marketSupplierList(List<SupplierEntity> supplierEntityList) {
        if (supplierEntityList != null & supplierEntityList.size() != 0) {
            for (SupplierEntity entity : supplierEntityList) {
                entity.setSupplierType(0);
            }
        }
        if (marketLoadMore) {
            if (supplierEntityList == null) {
                mDataBinding.marketRefreshLayout.finishLoadMore(false);
            } else if (supplierEntityList.size() < mViewModel.pageSize) {
                mDataBinding.marketRefreshLayout.finishLoadMore(true);
                mDataBinding.marketRefreshLayout.setEnableLoadMore(false);
                marketSupplierAdapter.addData(supplierEntityList);
            } else {
                mDataBinding.marketRefreshLayout.finishLoadMore(true);
                marketSupplierAdapter.addData(supplierEntityList);
            }
        } else {
            marketSelectedSupplierList.clear();
            mDataBinding.marketPoint.setVisibility(View.INVISIBLE);
            getSelectedSupplierList();
            if (supplierEntityList == null) {
                mDataBinding.marketRefreshLayout.setEnableLoadMore(false);
                mDataBinding.marketRefreshLayout.finishRefresh(false);
                mDataBinding.marketBlackView.setVisibility(View.VISIBLE);
                mDataBinding.marketRecyclerView.setVisibility(View.GONE);
                marketSupplierAdapter.setNewData(null);
            } else if (supplierEntityList.size() == 0) {
                mDataBinding.marketRefreshLayout.setEnableLoadMore(false);
                mDataBinding.marketRefreshLayout.finishRefresh(true);
                mDataBinding.marketBlackView.setVisibility(View.VISIBLE);
                mDataBinding.marketRecyclerView.setVisibility(View.GONE);
                marketSupplierAdapter.setNewData(supplierEntityList);
            } else if (supplierEntityList.size() < mViewModel.pageSize) {
                mDataBinding.marketRefreshLayout.setEnableLoadMore(false);
                mDataBinding.marketRefreshLayout.finishRefresh(true);
                mDataBinding.marketBlackView.setVisibility(View.GONE);
                mDataBinding.marketRecyclerView.setVisibility(View.VISIBLE);
                marketSupplierAdapter.setNewData(supplierEntityList);
            } else {
                mDataBinding.marketRefreshLayout.setEnableLoadMore(true);
                mDataBinding.marketRefreshLayout.finishRefresh(true);
                mDataBinding.marketBlackView.setVisibility(View.GONE);
                mDataBinding.marketRecyclerView.setVisibility(View.VISIBLE);
                marketSupplierAdapter.setNewData(supplierEntityList);
            }
        }
    }

    private void vipMemberSupplierList(List<SupplierEntity> supplierEntityList) {
        if (supplierEntityList != null & supplierEntityList.size() != 0) {
            for (SupplierEntity entity : supplierEntityList) {
                entity.setSupplierType(1);
            }
        }
        if (vipMemberLoadMore) {
            if (supplierEntityList == null) {
                mDataBinding.vipMemberRefreshLayout.finishLoadMore(false);
            } else if (supplierEntityList.size() < mViewModel.pageSize) {
                mDataBinding.vipMemberRefreshLayout.finishLoadMore(true);
                mDataBinding.vipMemberRefreshLayout.setEnableLoadMore(false);
                vipMemberSupplierAdapter.addData(supplierEntityList);
            } else {
                mDataBinding.vipMemberRefreshLayout.finishLoadMore(true);
                vipMemberSupplierAdapter.addData(supplierEntityList);
            }
        } else {
            vipMemberSelectedSupplierList.clear();
            mDataBinding.vipMemberPoint.setVisibility(View.INVISIBLE);
            getSelectedSupplierList();
            if (supplierEntityList == null) {
                mDataBinding.vipMemberRefreshLayout.setEnableLoadMore(false);
                mDataBinding.vipMemberRefreshLayout.finishRefresh(false);
                mDataBinding.vipMemberBlackView.setVisibility(View.VISIBLE);
                mDataBinding.vipMemberRecyclerView.setVisibility(View.GONE);
                vipMemberSupplierAdapter.setNewData(null);
            } else if (supplierEntityList.size() == 0) {
                mDataBinding.vipMemberRefreshLayout.setEnableLoadMore(false);
                mDataBinding.vipMemberRefreshLayout.finishRefresh(true);
                mDataBinding.vipMemberBlackView.setVisibility(View.VISIBLE);
                mDataBinding.vipMemberRecyclerView.setVisibility(View.GONE);
                vipMemberSupplierAdapter.setNewData(supplierEntityList);
            } else if (supplierEntityList.size() < mViewModel.pageSize) {
                mDataBinding.vipMemberRefreshLayout.setEnableLoadMore(false);
                mDataBinding.vipMemberRefreshLayout.finishRefresh(true);
                mDataBinding.vipMemberBlackView.setVisibility(View.GONE);
                mDataBinding.vipMemberRecyclerView.setVisibility(View.VISIBLE);
                vipMemberSupplierAdapter.setNewData(supplierEntityList);
            } else {
                mDataBinding.vipMemberRefreshLayout.setEnableLoadMore(true);
                mDataBinding.vipMemberRefreshLayout.finishRefresh(true);
                mDataBinding.vipMemberBlackView.setVisibility(View.GONE);
                mDataBinding.vipMemberRecyclerView.setVisibility(View.VISIBLE);
                vipMemberSupplierAdapter.setNewData(supplierEntityList);
            }
        }
    }

    private void memberSupplierList(List<SupplierEntity> supplierEntityList) {
        if (supplierEntityList != null & supplierEntityList.size() != 0) {
            for (SupplierEntity entity : supplierEntityList) {
                entity.setSupplierType(2);
            }
        }
        if (memberLoadMore) {
            if (supplierEntityList == null) {
                mDataBinding.memberRefreshLayout.finishLoadMore(false);
            } else if (supplierEntityList.size() < mViewModel.pageSize) {
                mDataBinding.memberRefreshLayout.finishLoadMore(true);
                mDataBinding.memberRefreshLayout.setEnableLoadMore(false);
                memberSupplierAdapter.addData(supplierEntityList);
            } else {
                mDataBinding.memberRefreshLayout.finishLoadMore(true);
                memberSupplierAdapter.addData(supplierEntityList);
            }
        } else {
            memberSelectedSupplierList.clear();
            mDataBinding.memberPoint.setVisibility(View.INVISIBLE);
            getSelectedSupplierList();
            if (supplierEntityList == null) {
                mDataBinding.memberRefreshLayout.setEnableLoadMore(false);
                mDataBinding.memberRefreshLayout.finishRefresh(false);
                mDataBinding.memberBlackView.setVisibility(View.VISIBLE);
                mDataBinding.memberRecyclerView.setVisibility(View.GONE);
                memberSupplierAdapter.setNewData(null);
            } else if (supplierEntityList.size() == 0) {
                mDataBinding.memberRefreshLayout.setEnableLoadMore(false);
                mDataBinding.memberRefreshLayout.finishRefresh(true);
                mDataBinding.memberBlackView.setVisibility(View.VISIBLE);
                mDataBinding.memberRecyclerView.setVisibility(View.GONE);
                memberSupplierAdapter.setNewData(supplierEntityList);
            } else if (supplierEntityList.size() < mViewModel.pageSize) {
                mDataBinding.memberRefreshLayout.setEnableLoadMore(false);
                mDataBinding.memberRefreshLayout.finishRefresh(true);
                mDataBinding.memberBlackView.setVisibility(View.GONE);
                mDataBinding.memberRecyclerView.setVisibility(View.VISIBLE);
                memberSupplierAdapter.setNewData(supplierEntityList);
            } else {
                mDataBinding.vipMemberRefreshLayout.setEnableLoadMore(true);
                mDataBinding.vipMemberRefreshLayout.finishRefresh(true);
                mDataBinding.memberBlackView.setVisibility(View.GONE);
                mDataBinding.memberRecyclerView.setVisibility(View.VISIBLE);
                memberSupplierAdapter.setNewData(supplierEntityList);
            }
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onClickSupplierItem(SupplierEntity entity) {
        KeyboardUtils.hideSoftInput(mDataBinding.goodsSearchSupplier);
        if (entity.isSelected()) {
            if ("0".equals(clickFlag)) {
                marketSelectedSupplierList.add(entity);
            } else if ("1".equals(clickFlag)) {
                vipMemberSelectedSupplierList.add(entity);
            } else if ("2".equals(clickFlag)) {
                memberSelectedSupplierList.add(entity);
            }
        } else {
            if ("0".equals(clickFlag)) {
                marketSelectedSupplierList.remove(entity);
            } else if ("1".equals(clickFlag)) {
                vipMemberSelectedSupplierList.remove(entity);
            } else if ("2".equals(clickFlag)) {
                memberSelectedSupplierList.remove(entity);
            }
        }
        if (marketSelectedSupplierList.size() > 0) {
            mDataBinding.marketPoint.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.marketPoint.setVisibility(View.INVISIBLE);
        }
        if (vipMemberSelectedSupplierList.size() > 0) {
            mDataBinding.vipMemberPoint.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.vipMemberPoint.setVisibility(View.INVISIBLE);
        }
        if (memberSelectedSupplierList.size() > 0) {
            mDataBinding.memberPoint.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.memberPoint.setVisibility(View.INVISIBLE);
        }
        getSelectedSupplierList();
    }


    private void getSelectedSupplierList() {
        allSelectedSupplierList.clear();
        allSelectedSupplierList.addAll(vipMemberSelectedSupplierList);
        allSelectedSupplierList.addAll(memberSelectedSupplierList);
        allSelectedSupplierList.addAll(marketSelectedSupplierList);
        removeDuplicate(allSelectedSupplierList);
        int selectCount = allSelectedSupplierList.size();
        if (selectCount == 0) {
            mDataBinding.right.setText("确定");
        } else {
            mDataBinding.right.setText("确定(" + selectCount + ")");
        }
        int allCount = selectCount + filterCompanyNos.size();
        marketSupplierAdapter.setSelectCount(allCount);
        vipMemberSupplierAdapter.setSelectCount(allCount);
        memberSupplierAdapter.setSelectCount(allCount);
    }

    /**
     * 移除相同得元素
     *
     * @param list
     * @return
     */
    private void removeDuplicate(ArrayList<SupplierEntity> list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(j).getNo().equals(list.get(i).getNo())) {
                    list.remove(j);
                }
            }
        }
    }

}